package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.business.vo.SearchAllVO;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.strategy.entity.Strategy;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import io.lettuce.core.dynamic.annotation.CommandNaming;
import org.elasticsearch.search.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class SearchController {

    @Autowired
    private ISearchService searchService;;

    @GetMapping("/search")
    public R<?> search(SearchQuery qo) throws UnsupportedEncodingException {

        String kw = URLDecoder.decode(qo.getKeyword(), "utf-8");
        qo.setKeyword(kw);
        switch (qo.getType()) {
            case 1:
                // 攻略
                return R.ok(searchStrategy(qo));
            case 2:
                // 游记
                return R.ok(searchNote(qo));
            case 3:
                // 用户
                return R.ok(searchUser(qo));
            case 4:
                // 问答
                return R.ok(searchAnswer(qo));
            default:
                return R.ok(searchAll(qo));
        }
    }

    private Object searchAll(SearchQuery qo) {
        Page<Strategy> strategyPage = searchService.searchHighLight(Strategy.class, StrategyEs.class, qo, "title", "subtitle", "summary");
        Page<Note> notePage = searchService.searchHighLight(Note.class, NoteEs.class, qo, "title",  "summary");
        Page<UserInfo> userInfoPage = searchService.searchHighLight(UserInfo.class, UserInfoEs.class, qo, "city",  "nickname","info");
        Page<TaDestination> destinationPaged = searchService.searchHighLight(TaDestination.class, DestinationEs.class, qo, "name","info");
        SearchAllVO vo = new SearchAllVO();
        vo.setDests(destinationPaged.getContent());
        vo.setNotes(notePage.getContent());
        vo.setStrategies(strategyPage.getContent());
        vo.setUsers(userInfoPage.getContent());
        vo.setTotal(strategyPage.getTotalElements()+notePage.getTotalElements()
        +destinationPaged.getTotalElements()+userInfoPage.getTotalElements());
        return vo;

    }

    private Object searchAnswer(SearchQuery qo) {
        return R.ok();
    }

    private Object searchUser(SearchQuery qo) {
        Page<UserInfo> userInfos = searchService.searchHighLight(UserInfo.class, UserInfoEs.class, qo, "city",  "nickname","info");
        return userInfos;
    }

    private Object searchNote(SearchQuery qo) {
        Page<Note> notes = searchService.searchHighLight(Note.class, NoteEs.class, qo, "title",  "summary");
        return notes;
    }

    private Object searchStrategy(SearchQuery qo) {
        Page<Strategy> strategies = searchService.searchHighLight(Strategy.class, StrategyEs.class, qo, "title", "subTitle", "summary");
        return strategies;

    }


}
